package com.rpp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Description
 * @Author rpp
 * @Date 2020/7/8 12:18 上午
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("toLogin")
    public String toLogin() {
        System.out.println("================++++++++++++++跳转登录页面");
        return "index";
    }

    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, String username, String password) {

        String contextPath = request.getContextPath();
        System.out.println("上下文路径：" + contextPath);

        if (!username.equals("admin") || !password.equals("admin")) {
            System.out.println("用户名密码不正确，跳转到登录页");
            return "redirect:" + contextPath + "/user/toLogin";
        }

        /*Cookie cookie = new Cookie(CookieUtil.MY_COOKIE_NAME, "123456");
        cookie.setPath("/");
        cookie.setMaxAge(60 * 30);
        cookie.setDomain("localhost");
        response.addCookie(cookie);*/

        System.out.println("用户名密码正确");
        session.setAttribute("username", username + "-" + System.currentTimeMillis());

        //跳转到另外一个Controller,模拟nginx转发后session是否共享
        return "redirect:/resume/list";
    }


}
